package com.ccapton.dragviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;

import com.ccapton.dragviewdemo.tag.RandomDragTagLayout;
import com.ccapton.dragviewdemo.tag.RandomDragTagView;
import com.ccapton.dragviewdemo.tag.TagModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DragTagLayoutActivity extends AppCompatActivity {
    Toolbar mToolbar;
    private String TAG = "RandomDragTagView";
    RandomDragTagLayout mRandomDragTagLayout;
    String mTagText = "自定义真难自定义真难自定义真难自定义真难自定义真难自定义真难自定义真难自定义真难";

    List<TagModel> mTagList = new ArrayList<>();
    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_tag_layout);


        seekBar = findViewById(R.id.seekBar);
        mRandomDragTagLayout = findViewById(R.id.tag_layout);

        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.addTagView(
                        mTagText.substring(0, new Random().nextInt(mTagText.length())),
                        TagModel.TEXT_TYPE,
                        0,0,
                        RandomDragTagView.COLOR_BLACK,
                        14,
                        new Random().nextFloat(),
                        new Random().nextFloat(),
                        new Random().nextBoolean()
                );

            }
        });

        findViewById(R.id.tv_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTag();
            }
        });

        findViewById(R.id.tv_restore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restoreTag();
            }
        });

        findViewById(R.id.tv_add_horizontal_line).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.addTagView(
                        "",
                        TagModel.LINE_TYPE,
                        0,0,
                        RandomDragTagView.COLOR_BLACK,
                        14,
                        new Random().nextFloat(),
                        new Random().nextFloat(),
                        new Random().nextBoolean()
                );
            }
        });
        findViewById(R.id.tv_add_vertical_line).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.addTagView(
                        "",
                        TagModel.LINE_TYPE_VERTICAL,
                        0,0,
                        RandomDragTagView.COLOR_BLACK,
                        14,
                        new Random().nextFloat(),
                        new Random().nextFloat(),
                        new Random().nextBoolean()
                );
            }
        });

        mRandomDragTagLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.e(TAG, "onTouch: " + mRandomDragTagLayout.getWidth() + "  " + mRandomDragTagLayout.getHeight());
                    Log.e(TAG, "onTouch: " + event.getX() + "  " + event.getY());
                    x = event.getX() / mRandomDragTagLayout.getWidth();
                    y = event.getY() / mRandomDragTagLayout.getHeight();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    Log.e(TAG, "onTouch: "+x+"  "+y );
                    mRandomDragTagLayout.addTagView(
                            " ",
                            TagModel.TEXT_TYPE,
                            0,0,
                            RandomDragTagView.COLOR_BLACK,
                            14,
                            x,
                            y,
                            new Random().nextBoolean()
                    );
                }
                return true;
            }
        });

        mRandomDragTagLayout.setOnSizeChangeListener(new RandomDragTagLayout.OnSizeChangeListener() {
            @Override
            public void onSizeChange(float size) {
                Log.e(TAG, "onSizeChange: "+size );
                seekBar.setProgress((int)size);
            }

            @Override
            public void onCLick() {

            }
        });

        findViewById(R.id.btn_red).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.getSelectView().setColor(RandomDragTagView.COLOR_RED);
            }
        });
        findViewById(R.id.btn_black).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.getSelectView().setColor(RandomDragTagView.COLOR_BLACK);
            }
        });
        findViewById(R.id.btn_gree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.getSelectView().setColor(RandomDragTagView.COLOR_GREEN);
            }
        });
        findViewById(R.id.btn_blue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.getSelectView().setColor(RandomDragTagView.COLOR_BLUE);
            }
        });
        findViewById(R.id.btn_orange).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRandomDragTagLayout.getSelectView().setColor(RandomDragTagView.COLOR_ORANGE);
            }
        });


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.e(TAG, "onProgressChanged: "+progress );
                mRandomDragTagLayout.getSelectView().setSize(progress);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private float x;
    private float y;

    private void saveTag() {
        mTagList.clear();
        for (int i = 0; i < mRandomDragTagLayout.getChildCount(); i++) {
            View childView = mRandomDragTagLayout.getChildAt(i);
            if (childView instanceof RandomDragTagView) {
                RandomDragTagView tagView = (RandomDragTagView) childView;
                TagModel tagModel = new TagModel();
                tagModel.type = tagView.getTagType();
                tagModel.direction = tagView.isShowLeftView();
                tagModel.text = tagView.getTagText();
                tagModel.x = tagView.getPercentTransX();
                tagModel.y = tagView.getPercentTransY();
                tagModel.width = tagView.getWidth();
                tagModel.color = tagView.getColor();
                tagModel.size = tagView.getSize();
                tagModel.height = tagView.getHeight();
                Log.e(TAG, "saveTag: "+tagView.getWidth()+"  "+tagView.getHeight() );
                mTagList.add(tagModel);
            }
        }
    }

    private void restoreTag() {
        if (!mTagList.isEmpty()) {
            mRandomDragTagLayout.removeAllViews();
            for (TagModel tagModel : mTagList) {
                mRandomDragTagLayout.addTagView(tagModel.text, tagModel.type,tagModel.width,tagModel.height, tagModel.color,(int)tagModel.size,tagModel.x, tagModel.y, tagModel.direction);
            }
        }
    }
}
